package com.jn.algorithm.other.question1124;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 江南大学1033190417
 * @date 2022/5/21 23:35
 */
public class Solution {
    public int longestWPI(int[] hours) {
        int n = hours.length;
        for (int i = 0; i < n; i++) {
            if (hours[i] > 8) {
                hours[i] = 1;
            } else {
                hours[i] = -1;
            }
        }
        //求前缀和
        List<Integer> preSum = new ArrayList<>();
        preSum.add(0);
        int curSum = 0;
        for (int i = 0; i < n; i++) {
            curSum += hours[i];
            preSum.add(curSum);
        }
        int max = 0;
        for (int i = 0; i < n; i++) {
            for (int j = n - 1; j >= i; j--) {
                if (j - i + 1 < max) {
                    continue;
                }
                if (preSum.get(j + 1) > preSum.get(i)) {
                    max = Math.max(max, j - i + 1);
                }
            }
        }
        return max;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[]{13, 4, 2, 5, 0, 6, 0, 10, 5, 12, 5};
        System.out.println(solution.longestWPI(nums));
    }
}
